##################################################################
# /* Author: Gautam Nair and Nicholas Sambanis */
# /* Violence Exposure and Ethnic Identification: Evidence from Kashmir */
# /* Table 3:  Average Treatment Effects Exposure to Violence*/

##################################################################

# change working directory here
# setwd("")

##################################################################
# loading packages
##################################################################
library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(plyr)
library(dplyr)
library(scales)

##################################################################

rm(list=ls())

library(stargazer)

data.working <- read.csv("nei_kashmir_replication_dataset.csv")

dep.var <- c("identity_rank_indian_rev", 
"identity_choice_indian_both", 
"kashmir_status_india",  
"ind_pak_prefer_ind",
"bonus_donation_india", 
"ic_index_id_no_ind_pak",
"ic_index_id_ind_pak",
"protest_peaceful_endorse",
"protest_violent_endorse"
)

ind.var <- c("group2_violence")

data.working <- data.working[data.working$group2_violence==1|data.working$group1_control==1,]

reg.models <-vector("list", length(dep.var)) 
se.models <-vector("list", length(dep.var))
for(i in 1:length(dep.var)){
	temp.data <- data.working[,c(dep.var[i],ind.var)]
	temp.data <- na.omit(temp.data)
	dep.var[i]
	my.formula <-paste(dep.var[i],'~', paste(ind.var, collapse= ' + '))
	temp.model <- lm(my.formula, data=temp.data)
	temp.se <- coeftest(temp.model, vcov=vcovHC(temp.model,type="HC2"))[,2]
	se.models[[i]] <- temp.se
	reg.models[[i]] <- temp.model
}

# stargazer customization

varlabels <-       c("Violence Exposure",  "Constant (Control Group Mean)")
spectitle <- c("Average Treatment Effects: Exposure to Violence")
output.file <- c("tf_t_03_ate_violence")
temptype <- c("latex", "text")
tempext <- c(".tex", ".txt")

for(q in 1:length(temptype)){
	temp.output <- paste(output.file, tempext[q], sep="")
	
	stargazer(reg.models,se=se.models,
         title= spectitle,
          no.space=TRUE, 
          model.numbers= TRUE,
         notes.append=FALSE,
          align=FALSE,
         covariate.labels=varlabels,
          header= FALSE,
          font.size="small",
          model.names=FALSE,
          digits=2,
          star.char=c("**", "***"),
          star.cutoffs=c(0.05, 0.01),
          omit.stat = c("rsq", "f","adj.rsq"),
          column.labels  = NULL,
          dep.var.caption  = "",
          dep.var.labels.include=FALSE,
         df=FALSE,
         omit.table.layout="n",
		out = temp.output,
		type= temptype[q]
	) 
}








